package com.baichang.taoshunda.service.impl;

import com.baichang.framework.excelption.ServiceException;
import com.baichang.framework.model.Page;
import com.baichang.taoshunda.dao.ScoreDao;
import com.baichang.taoshunda.dao.ScoreGoodsDao;
import com.baichang.taoshunda.dao.ScoreListDao;
import com.baichang.taoshunda.entity.ScoreList;
import com.baichang.taoshunda.service.ScoreListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * Created by ${wushenglu} on 2017/8/17 0017.
 */
@Service
public class ScoreListServiceImpl implements ScoreListService {
    @Autowired
    private ScoreListDao scoreListDao;
    @Autowired
    private ScoreGoodsDao scoreGoodsDao;
    @Autowired
    private ScoreDao scoreDao;

    /**
     * 添加积分明细
     *
     * @param point  分数
     * @param userId 用户编号
     * @param source 来源
     */
    @Override
    public void addScoreList(int point, Integer userId, String source) {
        ScoreList scoreList = new ScoreList();
        int allScore = scoreDao.getScoreByUserId(userId);
        //用户编号
        scoreList.setUserId(userId);
        //积分
        scoreList.setScore(point);
        //积分状态
        scoreList.setScoreState("+");
        //时间
        scoreList.setCreated(new Date());
        //来源
        scoreList.setSource(source);
        scoreList.setResult(allScore);
        scoreListDao.save(scoreList);
    }

    /**
     * 兑换积分商品
     *
     * @param id             商品编号
     * @param userId         用户编号
     * @param score          积分
     * @param address
     * @param goodsNumber
     * @param scoreGoodsName
     * @param addressName
     * @param addressUserName
     * @param addressUserPhone
     * @return
     */
    @Override
    public boolean convertScoreGoods(Integer id, Integer userId, Integer score, String address, Integer goodsNumber, String scoreGoodsName, String addressName, String addressUserName, String addressUserPhone) throws ServiceException {
        //查看剩余库存
        int number = scoreGoodsDao.getScoreGoodsStock(id);
        int allScore = scoreDao.getScoreByUserId(userId);
        //判断剩余库存是否大于0
        if (allScore >= score) {
            if (number - goodsNumber >= 0) {
                //减少对应的库存
                scoreGoodsDao.scoreGoodsStock(id, number - goodsNumber);
                ScoreList scoreList = new ScoreList();
                //用户编号
                scoreList.setUserId(userId);
                //积分
                scoreList.setScore(score);
                //积分商品编号
                scoreList.setConvertGoodsId(id);
                //兑换商品地址
                scoreList.setConvertAddress(address);
                //积分状态
                scoreList.setScoreState("-");
                //时间
                scoreList.setCreated(new Date());
                //添加兑换个数
                scoreList.setCount(goodsNumber);
                scoreList.setIsSendGoods(0);
                //来源
                scoreList.setSource("兑换积分商品");
                //剩余积分
                scoreList.setResult(allScore - score);

                scoreList.setAddress(addressName);

                scoreList.setAddressUserName(addressUserName);

                scoreList.setAddressUserPhone(addressUserPhone);
                //添加记录
                scoreListDao.save(scoreList);
                return scoreDao.updateScoreByUserId(userId, allScore - score) > 0;
            } else {
                throw new ServiceException("库存不足请注意兑换数量");
            }
        } else {
            throw new ServiceException("积分不足不能对换");
        }


    }

    /**
     * 积分兑换历史
     *
     * @param name
     * @param startCreate
     * @param account
     * @param nowPage
     * @param pageSize    @return
     */
    @Override
    public Page queryHistoryGoodsScore(String name, String startCreate, String endCreate, String account, int nowPage, int pageSize) {
        return scoreListDao.queryHistoryGoodsScore(name, startCreate, endCreate, account, nowPage, pageSize);
    }

    /**
     * 查询用户积分
     *
     * @param name
     * @param nowPage
     * @param pageSize
     * @return
     */
    @Override
    public Page<Map<String, Object>> queryUserScore(String name, int nowPage, int pageSize) {
        return scoreListDao.queryUserScore(name, nowPage, pageSize);
    }

    @Override
    public int getTodayCount(int userId) {

        return scoreListDao.getTodayCount(userId);
    }

    @Override
    public List<Map<String, Object>> convertList(Integer userId, Integer nowPage, Integer pageSize) {
        return scoreListDao.convertList(userId, nowPage, pageSize);
    }
}
